Thread: Standard alternative to sizeof(array)/sizeof(array[0])?

  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    Austria
    Posts
    10

    Standard alternative to sizeof(array)/sizeof(array[0])?

    Is there a good standard alternative to the
    Code:
    sizeof(array)/sizeof(array[0])
    used for determining the size of an array?

    Well sometimes you see:

    Code:
    sizeof array/sizeof*array
    but this is only somewhat shorter and not hardly better.

    What I'm looking for is a standard macro in a standard header, something like

    Code:
    #define lenof(a) (sizeof(a)/sizeof((a)[0]))
    but there is no such thing in stddef.h where offsetof is defined.
    There is the further problem, that
    Code:
    int foo(short array[8]) 
    {
       return lenof(array);
    }
    will return 2 not 8 on most targets, due to the fact that foo is equal to
    Code:
    int foo(short *array)
    which is a rather obscure feature of C.

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    There is no Standard alternative, as this has no need to standardize this.

    sizeof( array ) / sizeof( *array) is my personal favorite.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > which is a rather obscure feature of C.
    Nope - that's the perfectly normal behaviour of C.

    If there was a better way to work out how much memory a pointer pointed to, from simply looking at the pointer, we would all have been using it long ago (and myriad buffer overflow issues would never have happened).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Location
    Austria
    Posts
    10
    Quote Originally Posted by Salem View Post
    > which is a rather obscure feature of C.
    Nope - that's the perfectly normal behaviour of C.
    Well the 'obscure' thing is that C accepts
    Code:
    int foo(short bar[8])
    even as the number is redundant and unused. As the compiler does not care about the size of the array it shall insist on omitting it and complain on anything but:
    Code:
    int foo(short bar[])

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    There is a difference though between:

    int foo(short bar[])

    and

    int foo(short *bar)

    Cprogramming.com FAQ > Pointers And Arrays (intermediate)
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Location
    Austria
    Posts
    10
    Quote Originally Posted by claudiu View Post
    There is a difference though between:

    int foo(short bar[])

    and

    int foo(short *bar)

    Cprogramming.com FAQ > Pointers And Arrays (intermediate)
    No there is no difference in this case, as correctly stated in the FAQ:

    Rule 3:
    An array name in the declaration of a function parameter is
    treated by the compiler as a pointer to the first element of the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Standard efficiency
    By Jorl17 in forum C Programming
    Replies: 3
    Last Post: 06-18-2009, 11:48 AM
  2. array initialization & C standard
    By jim mcnamara in forum C Programming
    Replies: 2
    Last Post: 09-12-2008, 03:25 PM
  3. Bug in iterator comparison in C++ standard?
    By steev in forum C++ Programming
    Replies: 14
    Last Post: 07-12-2008, 12:02 AM
  4. C/C++ standard
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-06-2003, 03:22 AM
  5. standard language, standard compiler?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-03-2001, 04:21 AM